
using AutoMapper;
using DTO;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using SqlSugar;
using System.Collections.Generic;
using Util;

namespace API.Controllers
{
    public class SourceController : BaseController
    {
        Repository<tSource> _Source;
        readonly IMapper _mapper;
        ISqlSugarClient _db;
        public SourceController(Repository<tSource> Source, IMapper mapper, ISqlSugarClient db)
        {
            _Source = Source;
            _mapper = mapper;
            _db = db;
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultMsg<string>> Save([FromBody] SaveSourceInput dto)
        {
            ResultMsg<string> result = new ResultMsg<string>();
            try
            {
                var model = _mapper.Map<tSource>(dto);
                InitEntity(model);
                await _Source.InsertOrUpdateAsync(model);
                return result.Success();
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("SourceController--Save:" + ex.ToString());
                return result.Error(ex.ToString());
            }
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultMsg<List<SourceOutput>>> GetList([FromBody] SourceListInput dto)
        {
            ResultMsg<List<SourceOutput>> result = new ResultMsg<List<SourceOutput>>();
            try
            {
                if (dto.PageNum <= 0)
                {
                    return result.Error("PageNum不能小于或等于0");
                }
                if (dto.PageSize <= 0)
                {
                    return result.Error("PageSize不能小于或等于0");
                }
                SqlSugar.PageModel pageModel = new SqlSugar.PageModel
                {
                    PageIndex = dto.PageNum,
                    PageSize = dto.PageSize,
                };
                var exp = Expressionable.Create<tSource>();
                exp.And(t=>t.CreateTime>=dto.start&&t.CreateTime<=dto.end);
                var list = (await _Source.GetPageListAsync(exp.ToExpression(), pageModel)).OrderByDescending(x => x.CreateTime).ToList();
                var SourceOutputs = _mapper.Map<List<SourceOutput>>(list);
                return result.Success(SourceOutputs, pageModel.TotalCount);
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("SourceController--GetList:" + ex.ToString());
                return result.Error(ex.ToString());
            }
        }
        /// <summary>
        /// 获取唯一
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultMsg<SourceOutput>> GetTheOne(string id)
        {
            ResultMsg<SourceOutput> result = new ResultMsg<SourceOutput>();
            try
            {
                var model = await _Source.GetByIdAsync(id);
                var dao = _mapper.Map<SourceOutput>(model);
                return result.Success(dao);
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("SourceController--GetTheOne:" + ex.ToString());
                return result.Error(ex.ToString());
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultMsg<string>> Del(string id)
        {
            ResultMsg<string> result = new ResultMsg<string>();
            try
            {
                await _Source.DeleteByIdAsync(id);
                return result.Success();
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("SourceController--Del:" + ex.ToString());
                return result.Error(ex.ToString());
            }
        }

        /// <summary>
        /// 获取所有客户来源
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultMsg<List<SourceOutput>>> GetSourceAll()
        {
            ResultMsg <List<SourceOutput>> result= new ResultMsg<List<SourceOutput>>();
            try
            {
                List<tSource> list = await _Source.GetListAsync(t => t.FDeleted == false);
                var SourceOutputs = _mapper.Map<List<SourceOutput>>(list);

                return result.Success(SourceOutputs, list.Count);
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("SourceController--GetSourceAll:" + ex.ToString());
                return result.Exception();
            }
        }
    }
}
